package cn.golxen.barcode.service.impl;

import cn.golxen.barcode.entity.BarcodeInfo;
import cn.golxen.barcode.feign.GoodsFeign;
import cn.golxen.barcode.service.BarcodeService;
import cn.golxen.barcode.utli.APIUtlis;

import cn.golxen.commons.exception.BarcodeException;
import cn.golxen.commons.exception.BarcodeInfoException;
import cn.golxen.commons.vo.BarcodeInfoVo;
import org.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BarcodeServiceImpl implements BarcodeService {



//    private String getBarcode(String url) {
//        String res=apiUtlis.GetBarcode(url);
//        JSONObject jsonObject=new JSONObject(res);
//        JSONObject showapi_res_body=jsonObject.getJSONObject("showapi_res_body");
//        int ret_code=showapi_res_body.getInt("ret_code");
//        String ret_body=showapi_res_body.getString("ret_body");
//        JSONObject jsonObject1=new JSONObject(ret_body);
//        String text=jsonObject1.getJSONArray("codes_result").getJSONObject(0).getJSONArray("text").getString(0);
//        if (ret_code!=0)
//            throw new BarcodeException();
//        return text;
//    }


    @Override
    public String getCode(String url) {
        APIUtlis apiUtlis=new APIUtlis();
        String resBarcode=apiUtlis.GetBarcode(url);
        JSONObject jsonObject=new JSONObject(resBarcode);
        if(jsonObject.getInt("showapi_res_code")==-1)
            throw new BarcodeException();
        JSONObject showapi_res_body=jsonObject.getJSONObject("showapi_res_body");
        int ret_code=showapi_res_body.getInt("ret_code");
        if (ret_code!=0)
            throw new BarcodeException();
        String ret_body=showapi_res_body.getString("ret_body");
        JSONObject jsonObject1=new JSONObject(ret_body);
        if(jsonObject1.getInt("codes_result_num")==0)
            throw new BarcodeException();
        return jsonObject1.getJSONArray("codes_result").getJSONObject(0).getJSONArray("text").getString(0);
    }

    @Override
    public BarcodeInfoVo getBarcodeInfoByCode(String code) {
        APIUtlis apiUtlis=new APIUtlis();
        BarcodeInfo barcodeInfo=new BarcodeInfo();
        String resGoods=apiUtlis.GetGoodsInfo(code);
        JSONObject jsonObject2=new JSONObject(resGoods).getJSONObject("showapi_res_body");
        if(!jsonObject2.getString("ret_code").equals("0"))
            throw new BarcodeInfoException();
        barcodeInfo.setCode(code);
        barcodeInfo.setGoodsName(jsonObject2.getString("goodsName"));
        barcodeInfo.setTrademark(jsonObject2.getString("trademark"));
        if (!jsonObject2.getString("price").equals(""))
            barcodeInfo.setPrice(Double.parseDouble(jsonObject2.getString("price")));
        if (!jsonObject2.getString("img").equals(""))
            barcodeInfo.setUrl(jsonObject2.getString("img"));
        BarcodeInfoVo barcodeInfoVo=new BarcodeInfoVo();
        BeanUtils.copyProperties(barcodeInfo,barcodeInfoVo);
        return barcodeInfoVo;
    }

    @Override
    public BarcodeInfo getBarcodeInfo(String url) {
        APIUtlis apiUtlis=new APIUtlis();
        BarcodeInfo barcodeInfo=new BarcodeInfo();
        String resBarcode=apiUtlis.GetBarcode(url);
        JSONObject jsonObject=new JSONObject(resBarcode);
        if(jsonObject.getInt("showapi_res_code")==-1)
            throw new BarcodeException();
        JSONObject showapi_res_body=jsonObject.getJSONObject("showapi_res_body");
        int ret_code=showapi_res_body.getInt("ret_code");
        if (ret_code!=0)
            throw new BarcodeException();
        String ret_body=showapi_res_body.getString("ret_body");
        JSONObject jsonObject1=new JSONObject(ret_body);
        if(jsonObject1.getInt("codes_result_num")==0)
            throw new BarcodeException();
        String text=jsonObject1.getJSONArray("codes_result").getJSONObject(0).getJSONArray("text").getString(0);



        String resGoods=apiUtlis.GetGoodsInfo(text);
        JSONObject jsonObject2=new JSONObject(resGoods).getJSONObject("showapi_res_body");
        if(!jsonObject2.getString("ret_code").equals("0"))
            throw new BarcodeInfoException();
        barcodeInfo.setCode(text);
        barcodeInfo.setGoodsName(jsonObject2.getString("goodsName"));
        barcodeInfo.setTrademark(jsonObject2.getString("trademark"));
        if (!jsonObject2.getString("price").equals(""))
            barcodeInfo.setPrice(Double.parseDouble(jsonObject2.getString("price")));
        if (!jsonObject2.getString("img").equals(""))
            barcodeInfo.setUrl(jsonObject2.getString("img"));

        return barcodeInfo;
    }

//    public String QrParse(BufferedImage bImage) {
//        // TODO Auto-generated method stub
//        MultiFormatReader reader = new MultiFormatReader();
//        BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bImage)));
//        Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
////设置编码格式
//        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
//        try {
//            Result result = reader.decode(binaryBitmap,hints);
//
//            //System.out.println("二维码文本内容:"+result.getText());
//            return result.getText();
//        } catch (Exception e) {
//            //图片中不包含二维码 do not
//            //e.printStackTrace();
//            throw new BarcodeException();
//        }

//    }
}
